import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login'
import Main from '../views/Main'
import Home from '../views/Home.vue'
import store from '../store/index.js'
import {
	refreshToken
} from '../../api/system.js'
//导入NProgress进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'


Vue.use(VueRouter)

const menuRouter = [{
		path: "",
		redirect: "home"
	},
	{
		name: 'queryuser',
		path: 'queryuser',
		component: () => import('../views/user/QueryUser')
	},
	{
		name: 'adduser',
		path: 'adduser',
		component: () => import('../views/user/AddUser.vue')
	},
	{
		name: 'addclassroom',
		path: 'addclassroom',
		component: () => import('../views/classroom/AddClassroom.vue')
	},
	{
		name:'charts',
		path:'charts',
		component:()=>import('../views/classroom/Charts.vue')
	},
	{
		name: 'queryclassroom',
		path: 'queryclassroom',
		component: () => import('../views/classroom/QueryClassroom.vue')
	},
	{
		name: 'history',
		path: 'history',
		component: () => import('../components/History.vue')
	},
	{
		name: 'adminlist',
		path: 'adminlist',
		component: () => import('../components/AdminList.vue')
	},
	{
		name: 'home',
		path: 'home',
		component: Home
	},
	{
		name: 'personal',
		path: 'personal',
		component: () => import('../components/Personal.vue')
	},
	{
		name: 'notice',
		path: 'notice',
		component: () => import('../components/Notice.vue')
	},
];

const routes = [{
		path: '/',
		redirect: '/login'
	},
	{
		path: '/login',
		name: 'Login',
		component: Login
	},
	{
		path: '/main',
		component: Main,
		children: [
			...menuRouter
		]
	},
]

const router = new VueRouter({
	routes
})


// 路由导航守卫
router.beforeEach((to, from, next) => {
	if (from != undefined) {
		NProgress.start();
	}
	// to代表将要访问的路径
	// form代表从哪个路径来
	// next是一个函数 代表放行
	if (to.path === '/login') {
		return next();
	}
	const token = window.sessionStorage.getItem("bilibilibili");
	if (!token) {
		return next('/login');
	}
	next();
	// refreshToken().then(res => {
	// 	//没有获得新的token==null，
	// 	// 表示旧的token已经失效，需要重新登录
	// 	if (res.data1 == null && to.name !== 'login') {
	// 		next({
	// 			name: 'login'
	// 		}); //去登录界面
	// 		setJwtToken(''); //清空token
	// 	} else { 
	// 		setJwtToken(res.data1)
	// 		next();
	// 	}
	// })
});

router.afterEach((to, from) => {
	if ((from.path == '/' && to.path == '/login') || (from.path == '/main/home' && to.path == '/login')) {
		NProgress.done();
	} else {
		store.dispatch('addTab', to.path)
		NProgress.done();
	}
	// store.dispatch('addTab', to.path)

})

export default router
